package bigscreen

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-kratos/kratos/v2/log"
	"net/url"
	pb "phm/api/phm/v1/bigscreen"
	"phm/internal/biz"
	"phm/tools/javaApi"
	myStrings "phm/tools/strings"
)

type RadarBigscreenService struct {
	pb.UnimplementedRadarBigscreenServer
	radar *biz.RaRadarUseCase
	log   *log.Helper
}

func NewRadarBigscreenService(radar *biz.RaRadarUseCase, logger log.Logger) *RadarBigscreenService {
	return &RadarBigscreenService{radar: radar, log: log.NewHelper(logger)}
}

func (s *RadarBigscreenService) ExportRadarTypeFaultsNum(ctx context.Context, req *pb.ExportRadarTypeFaultsNumRequest) (*pb.ExportRadarTypeFaultsNumReply, error) {
	return &pb.ExportRadarTypeFaultsNumReply{}, nil
}
func (s *RadarBigscreenService) AdaptationParameters(ctx context.Context, req *pb.AdaptationParametersRequest) (*pb.AdaptationParametersReply, error) {
	return &pb.AdaptationParametersReply{}, nil
}
func (s *RadarBigscreenService) ExportSystemFaultsNum(ctx context.Context, req *pb.ExportSystemFaultsNumRequest) (*pb.ExportSystemFaultsNumReply, error) {
	return &pb.ExportSystemFaultsNumReply{}, nil
}
func (s *RadarBigscreenService) EnvironmentalParameters(ctx context.Context, req *pb.EnvironmentalParametersRequest) (*pb.EnvironmentalParametersReply, error) {
	return &pb.EnvironmentalParametersReply{}, nil
}
func (s *RadarBigscreenService) BusinessProcess(ctx context.Context, req *pb.BusinessProcessRequest) (*pb.BusinessProcessReply, error) {
	return &pb.BusinessProcessReply{}, nil
}
func (s *RadarBigscreenService) FixedRealTimeFiled(ctx context.Context, req *pb.FixedRealTimeFiledRequest) (*pb.FixedRealTimeFiledReply, error) {
	return &pb.FixedRealTimeFiledReply{}, nil
}
func (s *RadarBigscreenService) FixedSubSystemStatus(ctx context.Context, req *pb.RadarId) (*pb.FixedSubSystemStatusReply, error) {
	return &pb.FixedSubSystemStatusReply{}, nil
}
func (s *RadarBigscreenService) HistoricalData(ctx context.Context, req *pb.HistoricalDataRequest) (*pb.HistoricalDataReply, error) {
	return &pb.HistoricalDataReply{}, nil
}
func (s *RadarBigscreenService) RadarScoreHistoricalData(ctx context.Context, req *pb.RadarScoreHistoricalDataRequest) (*pb.RadarScoreHistoricalDataReply, error) {
	return &pb.RadarScoreHistoricalDataReply{}, nil
}
func (s *RadarBigscreenService) RadarTypeFaultsNum(ctx context.Context, req *pb.Result) (*pb.Result, error) {
	return &pb.Result{}, nil
}
func (s *RadarBigscreenService) RealTimeFiled(ctx context.Context, req *pb.FixedRealTimeFiledRequest) (*pb.RealTimeFiledReply, error) {
	// TODO: Implement RealTimeFiled.
	baseUrl := myStrings.NewStrBuilder("/pc/screen/v1/realTimeFiled?")
	if req.RadarId != "" {
		baseUrl.StrAppend("radarId=").StrAppend(req.RadarId)
	}
	if req.Field != "" {
		baseUrl.StrAppend("&field=").StrAppend(url.QueryEscape(req.Field))
	}
	resp, err := javaApi.GetJavaRpcServ(baseUrl.ToString())
	if err != nil {
		return nil, err
	}
	fmt.Println("the java res:", resp)
	var resBody = &javaApi.RespBody{}
	err = json.Unmarshal([]byte(resp), resBody)
	if err != nil {
		return nil, err
	}
	if resBody.Code != "200" {
		return nil, fmt.Errorf("code:%s,msg:%s", resBody.Code, resBody.Msg)
	}
	fmt.Println("the java res:", *resBody)
	var resMap = make(map[string]*pb.RealTimeFiledReply_RealTimeFiledItem)
	for key, val := range resBody.Data {
		tmp := &pb.RealTimeFiledReply_RealTimeFiledItem{}
		tmp.Data = make(map[string]string)
		for k, v := range val {
			tmp.Data[k] = fmt.Sprintf("%v", v)
		}
		resMap[key] = tmp
	}
	return &pb.RealTimeFiledReply{Data: resMap}, nil
}
func (s *RadarBigscreenService) RealTimeRadarScoreData(ctx context.Context, req *pb.RadarId) (*pb.RealTimeRadarScoreDataReply, error) {
	return &pb.RealTimeRadarScoreDataReply{}, nil
}
func (s *RadarBigscreenService) SubSystem(ctx context.Context, req *pb.RadarId) (*pb.SubSystemReply, error) {
	items, err := s.radar.GetRadarSubSystem(ctx, req.RadarId)
	if err != nil {
		return &pb.SubSystemReply{}, err
	}
	return &pb.SubSystemReply{SubSystems: items}, nil
}
func (s *RadarBigscreenService) SubSystemData(ctx context.Context, req *pb.SubSystemDataRequest) (*pb.SubSystemDataReply, error) {
	return &pb.SubSystemDataReply{}, nil
}
func (s *RadarBigscreenService) SubSystemMonthlyStatus(ctx context.Context, req *pb.SubSystemMonthlyStatusRequest) (*pb.SubSystemMonthlyStatusReply, error) {
	return &pb.SubSystemMonthlyStatusReply{}, nil
}
func (s *RadarBigscreenService) SubSystemStatus(ctx context.Context, req *pb.RadarId) (*pb.SubSystemStatusReply, error) {
	return &pb.SubSystemStatusReply{}, nil
}
func (s *RadarBigscreenService) SubSystemFaultsNum(ctx context.Context, req *pb.RadarId) (*pb.SubSystemFaultsNumReply, error) {
	return &pb.SubSystemFaultsNumReply{}, nil
}
